<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/9/24
 * Time: 13:06
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Information extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 我关注的人或所有用户---默认展示我关注的人，如果有搜索，就展示搜索的所有用户
     * @return \think\response\Json
     * keyword    搜索词
     */
    public function myAttent(){
        $keyword = input('keyword');
        $where['user_status'] = 1;
        if(isset($keyword) && $keyword != ''){
            $where['user_username'] = ['like', '%' . $keyword . '%'];
            $list = Db::table('up_u_user')
                ->field('user_id, user_username, user_image')
                ->where($where)
                ->select();
        }else{
            $where['att_my_user_id'] = $this->user_id;
            $list = Db::table('up_my_attent')
                ->join('up_u_user', 'att_other_user_id = user_id', 'left')
                ->where($where)
                ->field('user_id, user_username, user_image')
                ->order('att_time desc')
                ->select();
        }
        if($list){
            foreach($list as $k => $v){
                // 字母排序
                $list[$k]['letter'] = $this->Getzimu($list[$k]['user_username']);
            }
            $new_names = array_column($list, 'letter');
            array_multisort($new_names, SORT_ASC, $list);
            $start = '';
            $res = array();
            foreach ($list as $kk => $vv) {
                if ($vv['letter'] != $start) {
                    $start = $vv['letter'];
                }
                $res[$start][] = $vv;
            }
            $jing = array();
            if(isset($res['#'])){
                $jing['#'] = $res['#'];
                unset($res['#']);
            }
            $list = array_merge($res,$jing);
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 发布动态
     * @return \think\response\Json
     * info_content       动态内容
     * info_images        图片
     * info_video         视频
     * info_other_user    被@的用户
     */
    public function publishInformation(){
        $info_content = input('info_content');
        $info_images = input('info_images');
        $info_video = input('info_video');
        $info_other_user = input('info_other_user');
        $insert = [
            'info_user_id' => $this->user_id,
            'info_content' => $info_content,
            'info_time' => $this->create_time
        ];
        if(isset($info_images) && $info_images != ''){
            $insert['info_images'] = $info_images;
        }
        if(isset($info_video) && $info_video != ''){
            $insert['info_video'] = $info_video;
        }
        if(isset($info_other_user) && $info_other_user != ''){
            $insert['info_other_user'] = ',' . $info_other_user . ',';
        }
        
        Db::startTrans();
        try{
            // 插入动态表
            $info_id = Db::table('up_information')->insertGetId($insert);
            // 如果有@人，插入消息表
            if($info_other_user){
                $notice = [
                    'notice_title' => '动态@',
                    'notice_content' => '动态@',
                    'notice_user' => ',' . $info_other_user . ',',
                    'notice_type' => 6,
                    'notice_info_id' => $info_id,
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            Db::commit();
//            return json(['status' => 200, 'msg' => '发布成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->publishSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 动态列表
     * @return \think\response\Json
     * page    页码
     * call_cate   调用类型   1 动态列表   2 用户个人中心--动态列表
     * other_user_id     其他用户ID
     */
    public function infoList(){
        $call_cate = input('call_cate');
        $other_user_id = input('other_user_id');
        $where['info_del'] = 1;
        if($call_cate == 2){
            if(isset($other_user_id) && $other_user_id != ''){
                $userid = $other_user_id;
            }else{
                $userid = $this->user_id;
            }
            $where['info_user_id'] = $userid;
        }
        $list = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'left')
            ->where($where)
            ->field('info_id, user_id, user_username, user_image, info_content, info_images, 
            info_video, info_other_user, info_time, info_comment_num')
            ->page($this->page, 15)
            ->order('info_time desc')
            ->select();
        if($list){
            foreach($list as $k => $v){
                // 图片
                if(!empty($v['info_images'])){
                    $images = trim($v['info_images'], ',');
                    $list[$k]['info_images'] = explode(',', $images);
                }else{
                    $list[$k]['info_images'] = [];
                }
                // 点赞数
                $dz_num = Db::table('up_my_dz')->where('dz_info_id', $v['info_id'])->count();
                $list[$k]['dz_num'] = $dz_num;
                // 是否点赞
                $is_dz = Db::table('up_my_dz')
                    ->where('dz_user_id', $this->user_id)
                    ->where('dz_info_id', $v['info_id'])
                    ->find();
                if($is_dz){ // 已点赞
                    $list[$k]['is_dz'] = 1;
                }else{
                    $list[$k]['is_dz'] = 2;
                }
                // @的用户
                $info_other_user = trim($v['info_other_user'], ',');
                $user_name = Db::table('up_u_user')->where('user_id', 'IN', $info_other_user)->column('user_username');
                $list[$k]['user'] = $user_name;
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 点赞/取消点赞
     * @return \think\response\Json
     * info_id     动态ID
     */
    public function doDz(){
        $info_id = input('info_id');
        $exist = Db::table('up_my_dz')
            ->where('dz_user_id', $this->user_id)
            ->where('dz_info_id', $info_id)
            ->field('dz_id')
            ->find();
        Db::startTrans();
        try{
            if($exist){ // 取消点赞
                Db::table('up_my_dz')->delete($exist['dz_id']);
                // 删除消息表
                Db::table('up_notice')->where('notice_dz_id', $exist['dz_id'])->delete();
                $is_dz = 2; // 未点赞
            }else{
                $insert = ['dz_user_id' => $this->user_id, 'dz_info_id' => $info_id, 'dz_time' => $this->create_time];
                $dz_id = Db::table('up_my_dz')->insertGetId($insert);
                $is_dz = 1; // 已点赞
                
                // 插入消息表
                $info_user_id = Db::table('up_information')->where('info_id', $info_id)->value('info_user_id');
                $notice = [
                    'notice_title' => '动态点赞',
                    'notice_content' => '动态点赞',
                    'notice_user' => ',' . $info_user_id . ',',
                    'notice_type' => 10,
                    'notice_info_id' => $info_id,
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time,
                    'notice_dz_id' => $dz_id
                ];
                Db::table('up_notice')->insert($notice);
            }
            Db::commit();
            return json(['status' => 200, 'msg' => $is_dz]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 评论动态
     * @return \think\response\Json
     * comment_content     评论的内容
     * info_id             动态ID
     * comment_other_user  被@的用户ID
     */
    public function commentInfo(){
        $comment_content = input('comment_content');
        $info_id = input('info_id');
        $comment_other_user = input('comment_other_user');
        $insert = [
            'comment_user_id' => $this->user_id,
            'comment_info_id' => $info_id,
            'comment_content' => $comment_content,
            'comment_time' => $this->create_time
        ];
        if(isset($comment_other_user) && $comment_other_user != ''){
            $insert['comment_other_user'] = ',' . $comment_other_user . ',';
        }
        
        Db::startTrans();
        try{
            // 评论表
            $comment_id = Db::table('up_information_comment')->insertGetId($insert);
            // 评论数量+1
            Db::table('up_information')->where('info_id', $info_id)->setInc('info_comment_num');
            // 如果有@人，插入消息表
            if($comment_other_user){
                $notice = [
                    'notice_title' => '评论@',
                    'notice_content' => '评论@',
                    'notice_user' => ',' . $comment_other_user . ',',
                    'notice_type' => 7,
                    'notice_info_id' => $info_id,
                    'notice_comment_id' => $comment_id,
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => $this->create_time
                ];
                Db::table('up_notice')->insert($notice);
            }
            
            // 评论，插入消息表
            $info_user_id = Db::table('up_information')->where('info_id', $info_id)->value('info_user_id');
            $notice_comment = [
                'notice_title' => '评论我的动态',
                'notice_content' => '评论我的动态',
                'notice_user' => ',' . $info_user_id . ',',
                'notice_type' => 8,
                'notice_info_id' => $info_id,
                'notice_comment_id' => $comment_id,
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_time' => $this->create_time
            ];
            Db::table('up_notice')->insert($notice_comment);
            
            Db::commit();
//            return json(['status' => 200, 'msg' => '评论成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->commentSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 回复
     * @return \think\response\Json
     * reply_type        回复类型   1 评论   2 回复
     * reply_info_id     动态ID
     * comment_id        评论的ID
     * reply_reply_id    回复的回复ID,如果是评论，传0
     * reply_content     回复内容
     */
    public function replyComment(){
        $reply_type = input('reply_type'); // 1 回复的是评论   2 回复的是回复
        $reply_info_id = input('reply_info_id');
        $comment_id = input('comment_id');
        $reply_reply_id = input('reply_reply_id'); // 如果回复的是回复，传被回复的回复ID，如果是评论则传0
        $reply_content = input('reply_content');
        $insert = [
            'reply_type' => $reply_type,
            'reply_user_id' => $this->user_id,
            'reply_info_id' => $reply_info_id,
            'reply_comment_id' => $comment_id,
            'reply_reply_id' => $reply_reply_id,
            'reply_content' => $reply_content,
            'reply_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 插入回复表
            $insert_reply_id = Db::table('up_information_reply')->insertGetId($insert);
            // 评论数量+1
            Db::table('up_information')->where('info_id', $reply_info_id)->setInc('info_comment_num');
            // 插入消息表
            if($reply_type == 1){
                $notice_user_id = Db::table('up_information_comment')->where('comment_id', $comment_id)->value('comment_user_id');
            }else{
                $notice_user_id = Db::table('up_information_reply')->where('reply_id', $reply_reply_id)->value('reply_user_id');
            }
            $notice = [
                'notice_title' => '回复评论或回复',
                'notice_content' => '回复评论或回复',
                'notice_user' => ',' . $notice_user_id . ',',
                'notice_type' => 9,
                'notice_info_id' => $reply_info_id,
                'notice_comment_id' => $comment_id,
                'notice_reply_id' => $insert_reply_id,
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_time' => $this->create_time
            ];
            Db::table('up_notice')->insert($notice);
            
            Db::commit();
//            return json(['status' => 200, 'msg' => '回复成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->replySuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 动态详情
     * @return \think\response\Json
     * info_id     动态ID
     */
    public function informationDetail(){
        $info_id = input('info_id');
        $detail = Db::table('up_information')
            ->join('up_u_user', 'info_user_id = user_id', 'left')
            ->field('info_id, info_content, info_images, info_video, info_other_user, info_time, 
            user_username, user_image, info_comment_num, info_user_id')
            ->where('info_id', $info_id)
            ->find();
        // 图片
        if(!empty($detail['info_images'])){
            $images = trim($detail['info_images'], ',');
            $detail['info_images'] = explode(',', $images);
        }else{
            $detail['info_images'] = [];
        }
        // 点赞数
        $dz_num = Db::table('up_my_dz')->where('dz_info_id', $detail['info_id'])->count();
        $detail['dz_num'] = $dz_num;
        // 是否点赞
        $is_dz = Db::table('up_my_dz')
            ->where('dz_user_id', $this->user_id)
            ->where('dz_info_id', $detail['info_id'])
            ->find();
        if($is_dz){ // 已点赞
            $detail['is_dz'] = 1;
        }else{
            $detail['is_dz'] = 2;
        }
        // @的用户
        $info_other_user = trim($detail['info_other_user'], ',');
        $user_name = Db::table('up_u_user')->where('user_id', 'IN', $info_other_user)->column('user_username');
        $detail['info_user'] = $user_name;
        // 评论
        $comment = Db::table('up_information_comment')
            ->join('up_u_user', 'user_id = comment_user_id', 'left')
            ->where('comment_info_id', $detail['info_id'])
            ->where('comment_status', 1)
            ->field('comment_id, comment_content, user_username, user_image, comment_user_id, comment_other_user, 
            comment_time')
            ->page($this->page, 15)
            ->order('comment_time desc')
            ->select();
        
        if($comment){
            foreach($comment as $k => $v){
                // @的用户
                $comment_other_user = trim($v['comment_other_user'], ',');
                $user_name = Db::table('up_u_user')->where('user_id', 'IN', $comment_other_user)->column('user_username');
                $comment[$k]['comment_user'] = $user_name;
                // 回复
                $reply = Db::table('up_information_reply')
                    ->join('up_u_user', 'user_id = reply_user_id', 'left')
                    ->where('reply_comment_id', $v['comment_id'])
                    ->field('reply_id, reply_type, reply_comment_id, reply_reply_id, reply_content, 
                    reply_time, user_username, user_image')
                    ->select();
                if($reply){
                    foreach($reply as $kk => $vv){
                        if($vv['reply_type'] == 2){ // 回复的是回复
                            $to_user = Db::table('up_information_reply')
                                ->join('up_u_user', 'user_id = reply_user_id', 'left')
                                ->where('reply_id', $vv['reply_reply_id'])
                                ->field('user_username')
                                ->find();
                            $reply[$kk]['to_username'] = $to_user['user_username'];
                        }else{
                            $reply[$kk]['to_username'] = '';
                        }
                    }
                    $comment[$k]['reply'] = $reply;
                }else{
                    $comment[$k]['reply'] = [];
                }
            }
            $detail['comment'] = $comment;
        }else{
            $detail['comment'] = [];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 动态举报类型
     * @return \think\response\Json
     */
    public function infoReportType(){
        $type = Db::table('up_info_report_type')
            ->where('irt_del', 1)
            ->field('irt_id, irt_name')
            ->order('irt_time desc')
            ->select();
        if($type){
            return json(['status' => 200, 'msg' => $type]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提交动态举报
     * @return \think\response\Json
     * info_id     动态ID
     * ir_type_id  举报类型ID
     */
    public function doInfoReport(){
        $info_id = input('info_id');
        $ir_type_id = input('ir_type_id');
        $other_user_id = Db::table('up_information')->where('info_id', $info_id)->value('info_user_id');
        $insert = [
            'ir_type_id' => $ir_type_id,
            'ir_my_user_id' => $this->user_id,
            'ir_other_user_id' => $other_user_id,
            'ir_information_id' => $info_id,
            'ir_time' => $this->create_time
        ];
        $res = Db::table('up_info_report')->insert($insert);
        if($res){
//            return json(['status' => 200, 'msg' => '提交成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->commitSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 首字母
     * @param $str
     * @return string
     */
    private function Getzimu($str)
    {
        if(empty($str)){
            return '#';
        }
        $fchar = mb_substr($str, 0, 1, 'utf-8');
        // 判断表情    表情长度为4位
        if(strlen($fchar) >= 4 ){
            return '#';
        }
//        print_r(ord('z'));die;
//        $fchar=ord($str{0});
        if(ord($fchar) >= ord('A') && ord($fchar) <= ord('z')){
            return strtoupper($fchar);
        }
        $s1 = iconv('UTF-8', 'gb2312', $str);
        $s2 = iconv('gb2312', 'UTF-8', $s1);
        $s = $s2 == $str ? $s1 : $str;
        $asc = ord($s{0})*256+ord($s{1})-65536;
        if($asc >= -20319 && $asc <= -20284) return 'A';
        if($asc >= -20283 && $asc <= -19776) return 'B';
        if($asc >= -19775 && $asc <= -19219) return 'C';
        if($asc >= -19218 && $asc <= -18711) return 'D';
        if($asc >= -18710 && $asc <= -18527) return 'E';
        if($asc >= -18526 && $asc <= -18240) return 'F';
        if($asc >= -18239 && $asc <= -17923) return 'G';
        if($asc >= -17922 && $asc <= -17418) return 'H';
        if($asc >= -17417 && $asc <= -16475) return 'J';
        if($asc >= -16474 && $asc <= -16213) return 'K';
        if($asc >= -16212 && $asc <= -15641) return 'L';
        if($asc >= -15640 && $asc <= -15166) return 'M';
        if($asc >= -15165 && $asc <= -14923) return 'N';
        if($asc >= -14922 && $asc <= -14915) return 'O';
        if($asc >= -14914 && $asc <= -14631) return 'P';
        if($asc >= -14630 && $asc <= -14150) return 'Q';
        if($asc >= -14149 && $asc <= -14091) return 'R';
        if($asc >= -14090 && $asc <= -13319) return 'S';
        if($asc >= -13318 && $asc <= -12839) return 'T';
        if($asc >= -12838 && $asc <= -12557) return 'W';
        if($asc >= -12556 && $asc <= -11848) return 'X';
        if($asc >= -11847 && $asc <= -11056) return 'Y';
        if($asc >= -11055 && $asc <= -10247) return 'Z';
        return "#";
    }
}